<template>
  <div id="app">
    <!-- :key="$route.fullPath" -->
    <!--不能增加 key，否则 keepAlive 不生效 -->
    <router-view v-if="isRouterAlive" />
  </div>
</template>

<script>
import { browserRedirect, ipad, debounce } from '@/utils/tools';

export default {
  name: 'App',
  data() {
    return {
      isRouterAlive: true
    };
  },
  provide() {
    return {
      _reloadPage: this._reloadPage
    };
  },
  created() {
    const that = this;
    function removeReadOnly(classData = '.filterableSelect .el-input__inner') {
      that.$nextTick(() => {
        const filterableSelect = document.querySelectorAll(classData);
        if (filterableSelect && filterableSelect.length > 0) {
          filterableSelect.forEach(i => {
            i.removeAttribute('readOnly');
            i.onblur = function() {
              const _this = this;
              setTimeout(() => {
                _this.removeAttribute('readOnly');
              }, 200);
            };
          });
        }
      });
    }
    window.removeReadOnly = removeReadOnly;

    const remoteRequest = (className, cb) => {
      this.$nextTick(() => {
        const target = document.querySelector(`${className} .el-input__inner`);
        // select 搜索 移动端无法调起输入键盘bug处理
        // target.removeAttribute('readOnly');
        // target.onblur = () => {
        //   setTimeout(() => {
        //     this.removeAttribute('readOnly');
        //   }, 200);
        // };
        // 移动端 pad端 无法触发select远程搜索兼容
        if (!cb || typeof cb !== 'function') { return; }
        const fun = () => {
          cb(target.value);
        };
        if ((browserRedirect() || ipad()) && target) {
          target.oninput = debounce(fun, 500, false);
        }
      });
    };
    window.remoteRequest = remoteRequest;
  },
  mounted() {
  },
  methods: {
    freshVersion() { // 发版本后更新页面
      const { APP_BUILD_VERSION } = process.env;
      const localVersion = localStorage.getItem('VERSION');
      if (APP_BUILD_VERSION && localVersion && localVersion !== APP_BUILD_VERSION) {
        setTimeout(() => {
          location.reload(true);
          console.log('刷新页面');
        }, 300);
      }
      if (APP_BUILD_VERSION) localStorage.setItem('VERSION', APP_BUILD_VERSION);
    },
    _reloadPage() {
      this.isRouterAlive = false;
      this.$nextTick(function() {
        this.isRouterAlive = true;
      });
    }
  }
};
</script>
